/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Model.CategProd;
import Model.Promotions;
import Model.TradCateg;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author Anthares
 */
public class CategProdManager extends BaseManager {
    public CategProdManager(SessionFactory factory, String locale) {
        super(factory, locale);
    }

    public CategProd getCategProdByID(int id) {
        Session session = factory.openSession();
        CategProd cat = (CategProd) session.getNamedQuery("CategProd.findByCategId").setInteger("catedId", id).uniqueResult();
        cat.setTraduction(getTraduction(id));
        cat.setPromotionsList(getPromotions(id));
        session.close();
        return cat;
    }

    public List<CategProd> getAllCategProd() {
        Session session = factory.openSession();
        List<CategProd> list = (List<CategProd>) session.getNamedQuery("CategProd.findAll").list();
        for(CategProd categProd : list){
            categProd.setTraduction(getTraduction(categProd.getCategId()));
            categProd.setPromotionsList(getPromotions(categProd.getCategId()));
        }
        session.close();
        return list;
    }

    private TradCateg getTraduction(int id) {
        return new TradCategManager(factory, locale).getTradCategByID(id);
    }
    private List<Promotions> getPromotions(int categId){
        return new PromotionManager(factory, locale).getCurrentPromotionsByCateg(categId);
    }
}
